Functional genomics

Dr. Yu Cheng Hsu

Learning Objectives

  • Understand basic Python sybtax

Python for Genomics

  • Python is the industry standard for bioinformatics, computational biology, and AI.
  • Example applications:
    • Whether you are analyzing sequence data,
    • AlphaFold for protein structures,
    • Building phylogenetic trees
  • Highly Readable: Unlike older languages (like C++ or Java),
    • Designed to read almost like plain English.
  • Open source

Slide conventions

  • Use code formatting for special terms and examples, such as This or This are code.
# This is a code chunk
#| echo: true
#| eval: true
# This is a code chunk that Ray does not aim to let you execute

Variables

In python there two fundamental data type

  • Numbers (Pure values)
  • Strings (wrapped in quote marks ““)

We can store these values using = in a variable

  • Can only contain A-z, 0-9, and _
  • Must start with letter
  • Case-sensitive

Side notes: numbers can be further categorized by their storage methods

# The '#' symbol is a comment. Python ignores it. We use it to leave notes.
# Let's store a short snippet of a target gene:
target_gene_1 = "ATGCGTAACGTTAGCGCC"
target_gene_2 = "CCGCGATTGCAATGCGTA"

gene_1_expr = 15
gene_2_expr = 20

# To see what is inside our variable, we use the print function:
print(target_gene_1)
ATGCGTAACGTTAGCGCC
print(gene_1_expr)
15

Mathematical Operations

Python works exactly like a scientific calculator. The basic operators are:

  • addition (+)
  • subtraction (-)
  • multiplication (*)
  • division (/)
  • exponents (**)

Side notes: Do not skip multiplication signs as you do in math

# Simulating a PCR reaction
starting_templates = 5
pcr_cycles = 30

# The double asterisk (**) means "to the power of"
final_copies = starting_templates * (2 ** pcr_cycles)

print("Starting DNA templates:", starting_templates)
Starting DNA templates: 5
print("Final copies after 30 cycles:", final_copies)
Final copies after 30 cycles: 5368709120

Functions

Function is a predefined set of code to make you perform specific task.

function_name(input1, input_2, ...)

For example print() is a function to print out the string and values Here we introduce another function len() to inspect how long is the string

# Calculate the length of the target gene
seq_1_length = len(target_gene_1)
seq_2_length = len(target_gene_2)

# We can print text and variables together by separating them with a comma
print("Total base pairs (Seq 1):", seq_1_length)
Total base pairs (Seq 1): 18
print("Total base pairs (Seq 2):", seq_2_length)
Total base pairs (Seq 2): 18

Methods

Variables that store text (Strings) have special actions they can perform, called methods. We can ask our DNA sequence to count specific motifs or bases using .count().

# Count the number of Adenine bases in our sequence from Step 1
a_count = target_gene_1.count("A")
print("Number of A bases:", a_count)
Number of A bases: 4
# Count a specific motif (e.g., a CpG site)
cpg_count = target_gene_1.count("CG")
print("Number of CpG sites:", cpg_count)
Number of CpG sites: 3

Exercise: Calculating GC-Content

GC-content is crucial for determining the melting temperature of PCR primers (G-C base pairs form 3 hydrogen bonds, but A-T forms 2). Let’s calculate it using basic Python math

# Count the number of Adenine bases in our sequence from Step 1
a_count = target_gene_1.count("A")
print("Number of A bases:", a_count)
Number of A bases: 4
# Count a specific motif (e.g., a CpG site)
cpg_count = target_gene_1.count("CG")
print("Number of CpG sites:", cpg_count)
Number of CpG sites: 3

Replacing strings: Simulating Transcription

When DNA is transcribed to mRNA, Thymine (T) is replaced by Uracil (U). We can simulate this biological process instantly with the .replace() method.

# Transcribe the DNA to RNA by replacing T with U
mrna_sequence = target_gene_1.replace("T", "U")

print("Original DNA:", target_gene_1)
Original DNA: ATGCGTAACGTTAGCGCC
print("Transcribed mRNA:", mrna_sequence)
Transcribed mRNA: AUGCGUAACGUUAGCGCC

Bibliography Notes

Mondrian, Piet. 1935. Composition (No. 1) Gray-Red. Oil on canvas. the Art Institute of Chicago.